/**
 * <pre>
 * ss-fire-collector / LogViewHandler.java
 * collector.httpd.metad.logger Class
 * LogViewHandler
 * <br>
 * 
 * <br>
 * @author ytjeong/apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 27.
 * </pre>
 */
package collector.httpd.metad.logger;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.apexcns.core.util.LogUtil;

import org.eclipse.jetty.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import collector.core.util.DateUtil;
import collector.core.util.StringUtil;
import collector.httpd.HttpdUtil;

/** <pre>
 * Class : collector.httpd.metad.logger.LogViewHandler / LogViewHandler.java
 * 	Define Class Object : 
 * <br>
 * </pre>
 * @author ytjeong / apexcns,Inc.
 * @version 1.0
 * @see javadoc
 * @since 2012. 9. 27.
 */

public class LogViewHandler {
	private static Logger _log = LoggerFactory.getLogger(LogViewHandler.class);
//	private static final String _LogPrefixStr = CollectConstants.LOGGER_MODULE_NAME_SMC + CollectConstants.LOGGER_MODULE_NAME_AFTFIX;
	
//	private Config config = Config.newInstance();
//	private ConfigTask configTask = ConfigTask.newInstance();
	
	public void handle(String target, Request baseReq
			, HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		baseReq.setQueryEncoding("utf-8");
		baseReq.setHandled(true);

		LogViewRequest reqvo = new LogViewRequest();
		HttpdUtil.bindRequestBean(reqvo, request);
		//_log.debug(reqvo);
		doit(target, baseReq, request, response, reqvo);
	}
	
	private void doit(String target, Request baseReq
			, HttpServletRequest request, HttpServletResponse response
			, LogViewRequest reqvo)
	throws IOException, ServletException {
		try {
			//procTest(request, response);
			execute(request, response, reqvo);
		} catch (Exception e) {
		    _log.error(e.getMessage(), e);
		}
		
	}

	private void execute(HttpServletRequest request, HttpServletResponse response
			, LogViewRequest reqvo) throws Exception {

		String title = ":: Collector Log View ::";
		String subtitle = "";
		String logName = "state.log";
		
		String timeFormat = "yyyy-MM-dd HH:mm:ss";
		long currtime = System.currentTimeMillis();
		if(StringUtil.isEmpty(reqvo.getLogName())) {	// 로그명 미존재시, 기본 작업 로그 대상
		    	subtitle = "Default Log File View :: default, " + " LineNum=" + reqvo.getLineNum() ;
		} else {	// 로그명 존재시, 지정 로그 대상
		    	logName = reqvo.getLogName();
		    	subtitle = "Log File View :: " + logName + ", " + " LineNum=" + reqvo.getLineNum() ;
		}
//		JobStateInfoData jsid = JobTaskUtil.loadJobStateData(logName);
		
		// TODO::get Log Contents
		String msg = "[2012-09-27 11:57:00] INFO   -     ModuleJob.execute(114)     | col-JMC-Log>	| ------------------------------------------------------------------------------------" + "<br/>" 
			+ "[2012-09-27 11:57:00] INFO   -     ModuleJob.execute(140)     | col-JMC-Log>	| ----- Complete to Excute Task [TX_LOGGING_01] -------------------" + "<br/>"
			+ "[2012-09-27 11:57:28] DEBUG  - QuartzSchedulerThread.run(268)     | batch acquisition of 0 triggers";
		
		msg = LogUtil.getLogContents(logName);
		
		response.setStatus(HttpServletResponse.SC_OK);
		//response.setContentLength(1024);
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		StringBuilder strb = new StringBuilder();
		strb.append("<body onload=\"javascript:window.location.href='#thebottom'\">");
		strb.append("<h1>Report <b>" + title + "</b></h1>");
		strb.append("로드 시각 :: ").append(DateUtil.getDate(currtime, timeFormat)).append(" (").append(currtime).append(") miliseconds");
		
		strb.append("<h4>Title :: <b>" + subtitle + "</b></h4>");
		strb.append("" + msg + " ");
		
		strb.append("<span id='thebottom'><br/>----- End of Text ----- </span>");

		strb.append("로드 시각 :: ").append(DateUtil.getDate(currtime, timeFormat)).append(" (").append(currtime).append(") miliseconds");
		strb.append("<h4>Title :: <b>" + subtitle + "</b></h4>");
		
		strb.append("</body>");
		response.getWriter().println(strb.toString());

		return;
	}

}
